/*
start at 10:59 2005.12.18

一个八位数a,其如果能分解,最小的质因数最大为sqrt(a) 
*/

#include <stdio.h>
#include <string.h>
#include <math.h>

int zhi[2002];
int zcnt[2002];

bool iszhi(int a)
{
    int i , to;    
    if (a==2) return true;
    if (a%2 == 0) return false;
    to = (int)sqrt(a);
    for (i=3; i<=to; i+=2)
    	if (a%i == 0) return false;
   	return true;
}    

void predo()
{
    int i, j, a, b;
    zhi[1] = 2;
    zhi[2] = 3;
    zhi[0] = 2;
    i = 1;
    while (zhi[0] <= 2000)
    {
        a = 6*i - 1;
        b = 6*i + 1;
        if (iszhi(a)) zhi[ ++ zhi[0] ] = a;
        if (iszhi(b)) zhi[ ++ zhi[0] ] = b;
        i++;
    }
//    printf("the last is %d\n", zhi[2000]);
}

int count(int a)
{
    int i, j, re;
    re = 0;
    while (a)
    {
        re += a%10;
        a /= 10;
    }    
    return re;
}    

void precount()
{
    int i;
    for (i=1; i<=2000; ++i)
        zcnt[i] = count(zhi[i]);
}    

void checkout(int n)
{
    int nown;
    int i, j, tmp, to, bacn = n;
    bool nofind = true;
    
//    nown = count(n);
    to = (int) sqrt(n);
    while (nofind)
    {
        bacn ++;
        n = bacn;
        nown = count(n);
        tmp = 0;
        if (!iszhi(n))
        {
            for (i=1; i<=2000; ++i)
            {
                if (n%zhi[i] == 0)
                {
                    tmp += zcnt[i];
                    n /= zhi[i];
                    i = 0;
                }    
                if (n==1) break;
            }    
            if (n!=1) tmp += count(n);
            if (tmp == nown) nofind = false;
        }    
    }    // end of search
    printf("%d\n", bacn);
    
}    

int main()
{
    int n;
    predo();
    precount();
    
    while (scanf("%d", &n), n)
    {
        checkout(n);
    }    
    return 0;
}    